import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class FetchTaobao1688Task implements Runnable {
    static AtomicInteger atomicInteger = new AtomicInteger(0);
    CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
    LinkedBlockingQueue<String> idpaths;

    public FetchTaobao1688Task(LinkedBlockingQueue queue) {
        idpaths = queue;
    }

    /**
     * 抓取  每个目录对应的   product_type_files
     *
     * @param idPath
     * @throws IOException
     * @throws SQLException
     */
    public void fetch(String idPath) throws IOException, SQLException {
        if (org.apache.commons.lang.StringUtils.isEmpty(idPath)) return;
        try {
            String content = getContent("http://s.taobao.com/image?q=https%3A%2F%2Fcontestimg.wish.com%2Fapi%2Fwebimage%2F" + idPath + "-orignal.jpg", "taobao");
            long start = System.currentTimeMillis();
            String nid = RegUtils.match(content, "\"nid\":\"[0-9]{1,20}\"").get(0).substring(7).replace("\"", "");
            String content2 = getContent("https://s.1688.com/youyuan/collaboration_search.htm?tab=sameDesign&fromTaobao=true&fromOfferId=" + nid + "&from=search&autoSimilarTab=true", "1688");
            String offerListStr = RegUtils.match(content2, "offerList\":\\[.*\\],\"totalCount\"").get(0).replace(",\"totalCount\"", "").replace("offerList\":", "");
            JSONArray offerList = JSONObject.parseArray(offerListStr);
            if(offerList!=null){
                int i = 0;
                for (Object offer :offerList){
                    JSONObject o = (JSONObject) offer;
                    i++;
                }
            }
        } catch (Exception e) {

        }
    }

    private String getContent(String url, String type) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        if ("taobao".equals(type)) {
            addTaobaoHeader(httpGet);
        } else if ("1688".equals(type)) {
            add1688Header(httpGet);
        }
        CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet);
        String content = EntityUtils.toString(httpResponse.getEntity());
        httpResponse.close();
        if (content.contains("alisec")){
            System.out.println(content);
            throw new Exception("被ban了");
        }
        return content;
    }

    public static void addTaobaoHeader(HttpGet httpPost) {
        httpPost.addHeader("Host", "s.taobao.com");
        //httpPost.addHeader("Cookie","sails.sid=s%3AA1kVzZra915jJDEVc6_GsErDoX_3_5mK.TdG%2B5ByUluTJFXEuQP1%2B7coIH9Dz7QuvCxjnzD6np4w; user=%7B%22id%22%3A%22593fc60f341915cc15bdacb7%22%2C%22username%22%3A%22sorry%22%2C%22password%22%3A%22******%22%2C%22phone%22%3A%2215200920506%22%2C%22email%22%3A%22%22%2C%22companyName%22%3A%22%22%2C%22ownerInvitationCode%22%3A%22hVF9lz%22%2C%22loginCount%22%3A0%2C%22sessionTime%22%3A%222017-06-13%2019%3A01%3A35%22%7D; Hm_lvt_b067a69aef80b7d8a08f9f307667d672=1497344187; Hm_lpvt_b067a69aef80b7d8a08f9f307667d672=1497351778");
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        httpPost.addHeader("Accept", "*/*");
        httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
    }

    public static void add1688Header(HttpGet httpPost) {
        httpPost.addHeader("Host", "s.1688.com");
        //httpPost.addHeader("Cookie","sails.sid=s%3AA1kVzZra915jJDEVc6_GsErDoX_3_5mK.TdG%2B5ByUluTJFXEuQP1%2B7coIH9Dz7QuvCxjnzD6np4w; user=%7B%22id%22%3A%22593fc60f341915cc15bdacb7%22%2C%22username%22%3A%22sorry%22%2C%22password%22%3A%22******%22%2C%22phone%22%3A%2215200920506%22%2C%22email%22%3A%22%22%2C%22companyName%22%3A%22%22%2C%22ownerInvitationCode%22%3A%22hVF9lz%22%2C%22loginCount%22%3A0%2C%22sessionTime%22%3A%222017-06-13%2019%3A01%3A35%22%7D; Hm_lvt_b067a69aef80b7d8a08f9f307667d672=1497344187; Hm_lpvt_b067a69aef80b7d8a08f9f307667d672=1497351778");
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        httpPost.addHeader("Accept", "*/*");
        httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");

    }

    public void run() {
        String idpath;
        try {
            while ((idpath = idpaths.take()) != null) {
                fetch(idpath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException, SQLException {
        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<String>();
        List<String> strings = Files.readLines(new File("/Users/donar/Desktop/wish_itemId.txt"), Charsets.UTF_8);
        linkedBlockingQueue.addAll(strings);
        Executor executor = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++) {
            executor.execute(new FetchTaobao1688Task(linkedBlockingQueue));
        }
        //new FetchTaobao1688Task(linkedBlockingQueue).fetch("53ecca433e48ab098d56e3d1");

    }
}
